package com.teradata.reactor;

import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

public class FluxAsynDemo {

    @Test
    public void runInCurrentThread() throws InterruptedException{
        // 当前线程执行
        Flux.range(0,10)
                .publishOn(Schedulers.immediate())
                .subscribe(Utils::println);
        // 强制让主线程执行完毕
        Thread.currentThread().join(1000L);
    }

    @Test
    public void runOneThreadAsyn() throws InterruptedException {
        // 单线程异步执行
        Flux.range(0,10)
                .publishOn(Schedulers.single())
                .subscribe(Utils::println);
        // 强制让主线程执行完毕
        Thread.currentThread().join(1000L);
    }

    @Test
    public void runElasticThreadAsyn() throws InterruptedException {
        // 弹性线程池异步执行
        Flux.range(0,10)
                .publishOn(Schedulers.elastic())
                .subscribe(Utils::println);
        // 强制让主线程执行完毕
        Thread.currentThread().join(1000L);
    }

    @Test
    public void runMultiThreadAsyn() throws InterruptedException {
        // 并行线程池异步执行
        Flux.range(0,10)
                .publishOn(Schedulers.parallel())
                .subscribe(Utils::println);

        // 强制让主线程执行完毕
        Thread.currentThread().join(1000L);
    }
}

